/**
 * 导航守卫
 */
import router from './index'

import { whitePath } from '@/router/routes'
import LayoutBack from '@/layout/back/index.vue'

import backRoute from '@/router/modules/back'
import errorRoute from '@/router/modules/error'

import { getToken } from '@/utils/auth'

import useUserStore from '@/store/modules/user'
import { useRoutesStore } from '@/store/modules/routes'
import pinia from '@/store'

const userStore = useUserStore(pinia)
const routesStore = useRoutesStore(pinia)

// 匹配views里面所有的.vue文件
const modules = import.meta.glob('../views/**/*.vue')

function loadPageComponent(componentPath: string) {
  for (const key in modules) {
    if (key.endsWith(componentPath)) {
      return () => modules[key]()
    }
  }
}

router.beforeEach((to, from, next) => {
  document.title = to.meta.title as string || import.meta.env.VITE_APP_PROJECT_TITLE as string

  if (getToken()) {
    if (userStore.isLogin) {
      next()
    } else {

      // 设置已登录
      userStore.setIsLogin()

      // 添加动态路由
      const remoteRoutes = [
        {
          path: '/three',
          // component: LayoutBack,
          redirect: '/three/one',
          meta: { title: '远程模块三', hidden: false, icon: 'setting' },
          children: [
            {
              path: 'one',
              // component: () => import('@/views/three/one.vue'),
              componentPath: '/views/three/one.vue',
              meta: { title: '页面一', hidden: false, icon: 'setting' }
            },
            {
              path: 'two',
              // component: () => import('@/views/three/two.vue'),
              componentPath: '/views/three/two.vue',
              meta: { title: '页面二', hidden: false, icon: 'setting' }
            },
          ]
        },
        {
          path: '/four',
          // component: LayoutBack,
          redirect: '/four/one',
          meta: { title: '远程模块四', hidden: false, icon: 'setting' },
          children: [
            {
              path: 'one',
              // component: () => import('@/views/four/one.vue'),
              componentPath: '/views/four/one.vue',
              meta: { title: '页面一', hidden: false, icon: 'setting' }
            },
            {
              path: 'two',
              // component: () => import('@/views/four/two.vue'),
              componentPath: '/views/four/two.vue',
              meta: { title: '页面二', hidden: false, icon: 'setting' }
            },
          ]
        },
      ]

      // 添加远程路由
      for (let remoteRouteItem of remoteRoutes) {
        Object.assign(remoteRouteItem, { component: LayoutBack })
        if (remoteRouteItem.children) {
          for (let childItem of remoteRouteItem.children) {
            Object.assign(childItem, { component: loadPageComponent(childItem.componentPath) })
          }
        }
        router.addRoute(remoteRouteItem as any)
      }

      // 添加路由
      backRoute.forEach(item => {
        router.addRoute(item)
      })
      router.addRoute(errorRoute)

      // 初始化store中的路由
      if (routesStore.routes.length === 0) {
        routesStore.initRoutes()
      }

      // 添加新路由到store中
      routesStore.addRoutes(backRoute)
      routesStore.addRoutes(remoteRoutes as any)
      routesStore.addRoutes(errorRoute)

      next({ ...to, replace: true })
    }

  } else {
    if (whitePath.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }

})

router.afterEach((to, from) => {
  // ...
})